<?php

namespace app\common\validate\tysj;

class Store extends Base
{
	/**
     * 验证规则
     */
    protected $rule = [
        'id'          => 'require',
        'uid'         => 'require|unique:app\common\model\tysj\Store',
        'type'        => 'require|isOptions:StoreType',
    	'photo'       => 'require|max:100',
        'phone'       => 'require|regex:/^1[1-9]\d{9}$/',
        'name'        => 'require|max:32',
        'address'     => 'require|max:300',
        'description' => 'require|max:300',
        'is_invoice'  => 'require|number|isOptions:Invoice'
    ];
    /**
     * 提示消息
     */
    protected $message = [
        'id.require'          => '店铺ID不能为空',
        'id.isUser'           => '该店铺不属于您',
        'id.isShow'           => '店铺暂未开张',
        'id.isCompleted'      => '店铺未通过审核',
        'id.is2saolianshenhe' => '店铺状态',  
    	'uid.require'         => '用户ID不能为空',
        'uid.unique'          => '您已拥有店铺',
        'type.require'        => '店铺类型不能为空',
        'type.isOptions'      => '店铺类型不合法',
        'photo.require'       => '门头照不能为空',
        'photo.max'           => '门头照地址长度不能超过100',
        'phone.require'       => '店铺手机号码不能为空',
        'phone.regex'         => '店铺手机号不合法',
        'name.require'        => '店铺名称不能为空',
        'name.max'            => '店铺名称长度不能超过32',
        'address.require'     => '地址不能为空',
        'address.max'         => '地址长度不能超过300',
        'description.require' => '简介不能为空',
        'description.max'     => '简介长度不能超过300',
        'is_invoice.require'  => '是否支持开发票不能为空',
        'is_invoice.number'   => '是否支持开发票必须是一个数字',
        'is_invoice.isOptions'=> '是否支持开发票不合法'
    ];
    /**
     * 验证场景
     */
    protected $scene = [
        'save'             => ['uid', 'photo', 'name', 'address', 'description', 'is_invoice'],
        'look'             => ['id' => 'require|isShow:tysj\Store'],
        'status'           => ['id' => 'require|isUser:tysj\Store'],
        'info'             => ['id' => 'require|isUser:tysj\Store'],
        'preview'          => ['id' => 'require|isUser:tysj\Store'],
        'show'             => ['id' => 'isCompleted'],
        'is2saolianshenhe' => ['id' => 'require|isUser:tysj\Store|is2saolianshenhe']
    ];

    //验证数据审核是否已经完成
    protected function isCompleted($value, $rule, $data = []) {
        $model = loadOnly('Model')->getModel('tysj\Store');
        $enum = loadEnum('StoreExamine');
        $data = $model->where(['id' => $value])->find();
        return !!$data['sub_mchid'] && $data['examine'] == $enum->complete();
    }

    //验证是否可以进入扫脸审核状态
    protected function is2saolianshenhe($value, $rule, $data = []) {
        $model = loadOnly('Model')->getModel('tysj\Store');
        $enum = loadEnum('StoreExamine');
        $data = $model->where(['id' => $value])->find();
        return $data['examine'] == $enum->code2key('dengdaisaolianrenzheng');
    }
}